﻿
using System;
using System.Data;
using System.Collections.Generic;

namespace LDSP.BRL
{
    public partial class Codification : BYFW.BOL.BaseEntity
    {
        public Codification()
        {
            m_tableName = "t_codification";
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected int m_enterpriseID = 0;
        protected DateTime m_releaseDT = DateTime.Now;
        protected DateTime m_implementDT = DateTime.Now;
        protected bool m_isValid = true;
        protected int m_categoryID = 0;
        protected string m_organization = string.Empty;
        protected string m_referenceNumber = string.Empty;
        protected string m_fileName = string.Empty;
        protected string m_fileExtension = string.Empty;
        protected int m_catalogID = 0;
        protected DocType m_docType = DocType.法律法规;
        protected Enterprise m_enterprise = null;
        protected Category m_category = null;
        protected Catalog m_catalog = null;
        protected Priority m_priority = Priority.中;
        protected DateTime m_uploadDT = DateTime.Parse("1970-01-01");

        /// <summary>
        /// 企业ID
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
            set { m_enterpriseID = value; }
        }
        /// <summary>
        /// 发布时间
        /// </summary>
        public DateTime ReleaseDT
        {
            get { return m_releaseDT; }
            set { m_releaseDT = value; }
        }
        /// <summary>
        /// 实施时间
        /// </summary>
        public DateTime ImplementDT
        {
            get { return m_implementDT; }
            set { m_implementDT = value; }
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid
        {
            get { return m_isValid; }
            set { m_isValid = value; }
        }
        /// <summary>
        /// 类别ID
        /// </summary>
        public int CategoryID
        {
            get { return m_categoryID; }
            set { m_categoryID = value; }
        }
        /// <summary>
        /// 发布机构
        /// </summary>
        public string Organization
        {
            get { return m_organization; }
            set { m_organization = value; }
        }
        /// <summary>
        /// 发布文号
        /// </summary>
        public string ReferenceNumber
        {
            get { return m_referenceNumber; }
            set { m_referenceNumber = value; }
        }
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get { return m_fileName; }
            set { m_fileName = value; }
        }
        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string FileExtension
        {
            get { return m_fileExtension; }
            set { m_fileExtension = value; }
        }
        /// <summary>
        /// 栏目ID
        /// </summary>
        public int CatalogID
        {
            get { return m_catalogID; }
            set { m_catalogID = value; }
        }
        /// <summary>
        /// 文档类型
        /// </summary>
        public DocType DocType
        {
            get { return m_docType; }
            set { m_docType = value; }
        }
        /// <summary>
        /// 文档的优先级
        /// </summary>
        public Priority Priority
        {
            get { return m_priority; }
            set { m_priority = value; }
        }
        /// <summary>
        /// 上传时间
        /// </summary>
        public DateTime UploadDT
        {
            get { return m_uploadDT; }
            set { m_uploadDT = value; }
        }

        public Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (m_enterpriseID != 0)
                    {
                        m_enterprise.GetProperties(m_enterpriseID);
                    }
                }
                return m_enterprise;
            }
        }
        public Category Category
        {
            get
            {
                if (m_category == null)
                {
                    m_category = new Category();
                    if (m_categoryID != 0)
                    {
                        m_category.GetProperties(m_categoryID);
                    }
                }
                return m_category;
            }
        }
        public Catalog Catalog
        {
            get
            {
                if (m_catalog == null)
                {
                    m_catalog = new Catalog();
                    if (m_catalogID != 0)
                    {
                        m_catalog.GetProperties(m_catalogID);
                    }
                }
                return m_catalog;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("enterpriseID", m_enterpriseID);
            m_container.Add("releaseDT", m_releaseDT);
            m_container.Add("implementDT", m_implementDT);
            m_container.Add("isValid", m_isValid ? 1 : 0);
            m_container.Add("categoryID", m_categoryID);
            m_container.Add("organization", m_organization);
            m_container.Add("referenceNumber", m_referenceNumber);
            m_container.Add("fileName", m_fileName);
            m_container.Add("fileExtension", m_fileExtension);
            m_container.Add("catalogID", m_catalogID);
            m_container.Add("docType", (int)m_docType);
            m_container.Add("priority", (int)m_priority);
            m_container.Add("uploadDT", m_uploadDT);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            m_releaseDT = this.GetDateValue(record, "releaseDT");
            m_implementDT = this.GetDateValue(record, "implementDT");
            m_isValid = this.GetBoolValue(record, "isValid");
            m_categoryID = this.GetIntValue(record, "categoryID");
            m_organization = this.GetStringValue(record, "organization");
            m_referenceNumber = this.GetStringValue(record, "referenceNumber");
            m_fileName = this.GetStringValue(record, "fileName");
            m_fileExtension = this.GetStringValue(record, "fileExtension");
            m_catalogID = this.GetIntValue(record, "catalogID");
            m_docType = (DocType)this.GetIntValue(record, "docType");
            m_priority = (Priority)this.GetIntValue(record, "priority");
            m_uploadDT = this.GetDateValue(record, "uploadDT");
            return true;
        }

        public List<Codification> GetListByCatalogID(int catalogID)
        {
            string cond = string.Format("where {0}={1}", DB.GetCol("catalogID"), DB.GetVal(catalogID));
            return this.GetSpecialList<Codification>(null, cond);
        }

        /// <summary>
        /// 删除Instance，删除文件，删除企业的适用关系，失效关系的源和目标包含它的都删除
        /// </summary>
        public override int Delete(string transID)
        {
            //删除文件
            string fileFullPath = this.GetDownloadFileFullName();
            if (System.IO.File.Exists(fileFullPath))
            {
                System.IO.File.Delete(fileFullPath);
            }
            //删除失效关系
            new R_DisableDoc().DeleteBySourceAndDestination(m_id);
            //删除适用关系
            List<R_EnterpriseCodification> list = new R_EnterpriseCodification().GetListByCodification(m_id);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Delete();
            }
            return base.Delete(transID);
        }

        public string GetDownloadFileFullName()
        {
            string dir = System.Web.HttpContext.Current.Request.MapPath("~/Docs");
            return System.IO.Path.Combine(dir, m_fileExtension);
        }

        public DataTable GetRecordsByEnterpriseID(int enterpriseID)
        {
            string cond = string.Format("where {0}={1}", DB.GetCol("enterpriseID"), DB.GetVal(enterpriseID));
            return this.GetRecords(cond);
        }
    }
}
